﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using CardsServer.DAL;
using System.Linq.Expressions;
using LinqToSqlExtensions;

namespace CardsServer.WCF
{
    public partial interface IService
    {
        [OperationContract]
        string ProductSale_D_Add(ProductSale_D model, int ACID);

        /// <summary>
        /// 通过销售单单查询销售单明细
        /// </summary>
        /// <param name="djn"></param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        [OperationContract]
        List<View_ProductSale_D> Get_ProductSale_D_By_M(string djn, int ACID);

        /// <summary>
        /// 通过单据删除单据明细
        /// </summary>
        /// <param name="djn"></param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        [OperationContract]
        void ProductSale_D_DeleteByDjn(string djn, int ACID);

        /// <summary>
        /// 通过ID删除据明细
        /// </summary>
        /// <param name="djn"></param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        [OperationContract]
        void ProductSale_D_DeleteByID(int zid, int ACID);

        /// <summary>
        /// 通过销售单单查询销售单明细
        /// </summary>
        /// <param name="djn"></param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        [OperationContract]
        List<ProductSale_D> Get_ProductSale_DList_By_M(string djn, int ACID);

        /// <summary>
        /// 销售出库单明细表
        /// </summary>
        /// <param name="btime"></param>
        /// <param name="etime"></param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        [OperationContract]
        List<View_ProductSale_D_List> Get_ProductSale_D_List(DateTime? btime,DateTime? etime, Guid? opid, Guid? jsid, Guid? clientid,int ACID);
        
    }
    public partial class Service :IService
    {

        #region  添加ProductSale_D表数据
        /// <summary>
        /// 添加ProductSale_D
        /// </summary>
        /// <param name="model">ProductSale_D实体</param>
        /// <returns>空或错误</returns>
        public string ProductSale_D_Add(ProductSale_D model, int ACID)
        {
            if (string.IsNullOrEmpty(model.djn))
            {
                return "『单据号』不能为空！";
            }
            if (string.IsNullOrEmpty(model.goodscode))
            {
                return "『产品编号』不能为空！";
            }
            if (model.count == 0)
            {
                return "『数量』不能为空！";
            }
            if (model.jsamount == 0)
            {
                return "『结算价格』不能为空！";
            }
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.ProductSale_D.Count(p => p.djn == model.djn) > 0)
                    {
                        return "主键已存在！";
                    }
                    ctx.ProductSale_D.InsertOnSubmit(model);
                    ctx.SubmitChanges();
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    return " 添加失败!错误详细信息：" + ex.Message;
                }
            }
        }
        #endregion

        #region  添加ProductSale_D表数据事物用重构
        /// <summary>
        /// 添加ProductSale_D事物用重构
        /// </summary>
        /// <param name="model">ProductSale_D实体</param>
        /// <returns>空或错误</returns>
        private string ProductSale_D_Add(ProductSale_D model, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            if (string.IsNullOrEmpty(model.djn))
            {
                return "『单据号』不能为空！";
            }
            if (string.IsNullOrEmpty(model.goodscode))
            {
                return "『产品编号』不能为空！";
            }
            if (model.count == null || model.count == 0)
            {
                return "『数量』不能为空！";
            }
            if (model.jsamount == null || model.jsamount == 0)
            {
                return "『结算价格』不能为空！";
            }
            //if (ctx.ProductSale_D.Count(p => p.zid == model.zid) > 0)
            //{
            //    return "主键已存在！";
            //}
            ctx.ProductSale_D.InsertOnSubmit(model);
            return string.Empty;
        }
        #endregion


        public List<View_ProductSale_D> Get_ProductSale_D_By_M(string djn, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext cxt = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                Expression<Func<View_ProductSale_D, bool>> predicate = PredicateBuilder.True<View_ProductSale_D>();
                predicate = p => p.djn == djn; //销售单据号
                return cxt.View_ProductSale_D.Where(predicate).ToList();
            }
        }


        #region 删除ProductSale_D表数据-通过单据号

        public void ProductSale_D_DeleteByDjn(string djn, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    List<ProductSale_D> pdlist = ctx.ProductSale_D.Where(p => p.djn == djn).ToList();
                    foreach (ProductSale_D pditem in pdlist)
                    {
                        ctx.ProductSale_D.DeleteOnSubmit(pditem);
                    }
                    ctx.SubmitChanges();
                }
                catch
                {

                }
            }
        }
        #endregion

        #region 删除ProductSale_D表数据-通过单据号 事物重构

        public void ProductSale_D_DeleteByDjn(string djn, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {

            try
            {
                List<ProductSale_D> pdlist = ctx.ProductSale_D.Where(p => p.djn == djn).ToList();
                foreach (ProductSale_D pditem in pdlist)
                {
                    ctx.ProductSale_D.DeleteOnSubmit(pditem);
                }
            }
            catch
            {

            }

        }
        #endregion


        public void ProductSale_D_DeleteByID(int zid, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    ProductSale_D model = ctx.ProductSale_D.Single(p => p.zid == zid);
                    ctx.ProductSale_D.DeleteOnSubmit(model);
                    ctx.SubmitChanges();
                }
                catch
                {

                }
            }
        }

        #region  删除ProductSale_D表Bydid数据[事物用重构]
        /// <summary>
        /// 删除KCSale_DBydid[事物用重构]
        /// </summary>
        /// <param name="did">详单流水号</param>
        /// <returns>空或错误</returns>
        private string ProductSale_D_DeleteByID(int zid, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {

            ProductSale_D model = ctx.ProductSale_D.Single(p => p.zid == zid);
            ctx.ProductSale_D.DeleteOnSubmit(model);
            return string.Empty;
        }
        #endregion


        public List<ProductSale_D> Get_ProductSale_DList_By_M(string djn, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext cxt = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                Expression<Func<ProductSale_D, bool>> predicate = PredicateBuilder.True<ProductSale_D>();
                predicate = p => p.djn == djn; //销售单据号
                return cxt.ProductSale_D.Where(predicate).ToList();
            }
        }


        public List<View_ProductSale_D_List> Get_ProductSale_D_List(DateTime? btime,DateTime? etime, Guid? opid, Guid? jsid, Guid? clientid, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext cxt = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                Expression<Func<View_ProductSale_D_List, bool>> predicate = PredicateBuilder.True<View_ProductSale_D_List>();

                if (btime != null && etime != null)
                {
                    predicate = predicate.And(p => p.odate.Date >= btime && p.odate.Date <= etime);
                }
                if (opid.HasValue) //制单人
                {
                    predicate = predicate.And(p => p.createuser == opid);
                }

                if (jsid.HasValue) //业务员 经手人
                {
                    predicate = predicate.And(p => p.jsuser == jsid);
                }

                if (clientid.HasValue)
                {
                    predicate = predicate.And(p => p.clientID == clientid);
                }
                return cxt.View_ProductSale_D_List.Where(predicate).ToList();
            }
        }
    }
}
